
package com.apes.fn.crm.ReturnVisit.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 售后回访员
 *
 * @author bbyford
 * @create 2020-2-11
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicUpdate
@DynamicInsert
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = VisitTransferHistory.CONST_TABLE_NAME)
public class VisitTransferHistory extends Model {
    public static final String CONST_TABLE_NAME = "fn_visit_transfer_history";

    @Id
    @Comment("编码")
    @Column(name = "id", nullable = false, length = 32)
    @ApesGenerator(code = "fn_visit_transfer_history", name = "售后回访转移历史记录")
    private String id;

    @Comment("公司")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id", nullable = false)
    private PartyRole company;

    @Comment("转移人")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "transfer_id", referencedColumnName = "id", nullable = false)
    private PartyRole transferId;

    @Comment("转移原因")
    @Column(name = "transfer_reason", length = 4000)
    private String transferReason;

    @Comment("转移日期")
    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "transfer_date", nullable = false)
    private Date transferDate;

    /***
     * 功能： 售后回访明细
     */
    @OneToMany(mappedBy = "visitTransferHistory", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<VisitTransferHistoryItem> visitTransferHistoryItems = new ArrayList();

    public List<VisitTransferHistoryItem> getVisitTransferHistoryItems() {
        if (visitTransferHistoryItems == null) return null;
        for (VisitTransferHistoryItem item : visitTransferHistoryItems) {
            item.setVisitTransferHistory(this);
        }
        return visitTransferHistoryItems;
    }

}


